{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Latest Time by Replacing Hidden Digits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maximumTime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #替换隐藏数字得到的最晚时间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>time</code> ，格式为 <code> hh:mm</code>（小时：分钟），其中某几位数字被隐藏（用 <code>?</code> 表示）。</p>\n",
    "\n",
    "<p>有效的时间为 <code>00:00</code> 到 <code>23:59</code> 之间的所有时间，包括 <code>00:00</code> 和 <code>23:59</code> 。</p>\n",
    "\n",
    "<p>替换 <code>time</code> 中隐藏的数字，返回你可以得到的最晚有效时间。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>time = \"2?:?0\"\n",
    "<strong>输出：</strong>\"23:50\"\n",
    "<strong>解释：</strong>以数字 '2' 开头的最晚一小时是 23 ，以 '0' 结尾的最晚一分钟是 50 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>time = \"0?:3?\"\n",
    "<strong>输出：</strong>\"09:39\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>time = \"1?:22\"\n",
    "<strong>输出：</strong>\"19:22\"\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>time</code> 的格式为 <code>hh:mm</code></li>\n",
    "\t<li>题目数据保证你可以由输入的字符串生成有效的时间</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [latest-time-by-replacing-hidden-digits](https://leetcode.cn/problems/latest-time-by-replacing-hidden-digits/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [latest-time-by-replacing-hidden-digits](https://leetcode.cn/problems/latest-time-by-replacing-hidden-digits/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"2?:?0\"', '\"0?:3?\"', '\"1?:22\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans = list()\n",
    "        for i in range(5):\n",
    "            if time[i] == '?':\n",
    "                if i == 0:\n",
    "                    if ord(time[i + 1]) < ord('4') or time[i + 1] == '?': ans.append('2')\n",
    "                    else: ans.append('1')\n",
    "                elif i == 1:\n",
    "                    if ans[-1] == '2': ans.append('3')\n",
    "                    else: ans.append('9')\n",
    "                elif i == 3:\n",
    "                    ans.append('5')\n",
    "                else: ans.append('9')\n",
    "            else:\n",
    "                ans.append(time[i])\n",
    "        return \"\".join(a for a in ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        lst = list(time)\n",
    "\n",
    "        if lst[0] == lst[1] == '?':\n",
    "            lst[0] = '2'\n",
    "            lst[1] = '3'\n",
    "        elif lst[0] == '?':\n",
    "            if int(lst[1])>=4:\n",
    "                lst[0] = '1'\n",
    "            else:\n",
    "                lst[0] = '2'\n",
    "        elif lst[1] == '?':\n",
    "            if int(lst[0]) == 2:\n",
    "                lst[1] = '3'\n",
    "            else:\n",
    "                lst[1] = '9'\n",
    "        \n",
    "        if lst[3] == '?':\n",
    "            lst[3] = '5'\n",
    "        if lst[4] == '?':\n",
    "            lst[4] = '9'\n",
    "\n",
    "        return ''.join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for i in range(len(time)): \n",
    "            if time[i] == \"?\": \n",
    "                if i == 0: \n",
    "                    time[i] = \"2\" if time[i+1] in \"?0123\" else \"1\"\n",
    "                elif i == 1: \n",
    "                    time[i] = \"3\" if time[0] == \"2\" else \"9\"\n",
    "                elif i == 3: \n",
    "                    time[i] = \"5\"\n",
    "                else: \n",
    "                    time[i] = \"9\"\n",
    "                    \n",
    "        return \"\".join(time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        hour = {'1?':'19:', '0?':'09:', '2?':'23:', '??':'23:'}\n",
    "        if time[0:2] in hour:\n",
    "            res = hour[time[0:2]]\n",
    "        else:\n",
    "            if time[0] != '?':\n",
    "                res = time[0:3]\n",
    "            else:\n",
    "                if time[1] in '0123':\n",
    "                    res = '2' + time[1] + ':'\n",
    "                else:\n",
    "                    res = '1' + time[1] + ':'\n",
    "        if time[3:] == '??':\n",
    "            res += '59'\n",
    "        elif time[3] == '?':\n",
    "            res += '5' + time[4]\n",
    "        elif time[4] == '?':\n",
    "            res += time[3] + '9'\n",
    "        else:\n",
    "            res += time[3:]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        h, m = time.split(':')\n",
    "        if h == '??':\n",
    "            h = '23'\n",
    "        elif h[0] == '?':\n",
    "            if h[1] <= '3':\n",
    "                h = '2' + h[1]\n",
    "            else:\n",
    "                h = '1' + h[1]\n",
    "        elif h[1] == '?':\n",
    "            if h[0] == '2':\n",
    "                h = '23'\n",
    "            else:\n",
    "                h = h[0] + '9'\n",
    "        \n",
    "        if m == '??':\n",
    "            m = '59'\n",
    "        elif m[0] == '?':\n",
    "            m = '5' + m[1]\n",
    "        elif m[1] == '?':\n",
    "            m = m[0] + '9'\n",
    "        return h + ':' + m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = [x for x in time]\n",
    "        if time[4] == '?':\n",
    "            time[4] = '9'\n",
    "        if time[3] == '?':\n",
    "            time[3] = '5'\n",
    "        if time[0] == '?':\n",
    "            if time[1] < '4' or time[1] == '?':\n",
    "                time[0] = '2'\n",
    "            else:\n",
    "                time[0] = '1'\n",
    "        if time[1] == '?':\n",
    "            if time[0] < '2':\n",
    "                time[1] = '9'\n",
    "            else:\n",
    "                time[1] = '3'\n",
    "        return ''.join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        \"\"\"\n",
    "        time: str\n",
    "        output:max time\n",
    "        \"\"\"\n",
    "        input_time=list(time)\n",
    "        output_time = input_time\n",
    "        for i in range(len(input_time)):\n",
    "            if \"?\" == input_time[i]:\n",
    "                if i == 0 and (input_time[1] <= \"3\" or input_time[1]==\"?\"):\n",
    "                    output_time[0]=\"2\"\n",
    "                elif i == 0 and input_time[1] > \"3\":\n",
    "                    output_time[0]=\"1\"\n",
    "                elif i == 1 and input_time[0] != \"2\":\n",
    "                    output_time[1]=\"9\"\n",
    "                elif i == 1 and input_time[0] == \"2\":\n",
    "                    output_time[1]=\"3\"              \n",
    "                elif i == 3:\n",
    "                    output_time[3]=\"5\"\n",
    "                elif i == 4:\n",
    "                    output_time[4]=\"9\"\n",
    "        return(\"\".join(output_time))\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        t = list(time)\n",
    "        lst = [i for i in range(len(time)) if time[i] == \"?\"]\n",
    "        for n in lst:\n",
    "            match n:\n",
    "                case 0:\n",
    "                    t[n] = \"1\" if t[1] != \"?\" and int(t[1]) > 3 else \"2\"\n",
    "                case 1:\n",
    "                    t[n] = \"9\" if t[0] == \"1\" or t[0] == \"0\" else \"3\"\n",
    "                case 3:\n",
    "                    t[n] = \"5\"\n",
    "                case 4:\n",
    "                    t[n] = \"9\"\n",
    "        return \"\".join(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        lastest = list(time)\n",
    "\n",
    "        if lastest[0] == '?':\n",
    "            if (lastest[1] >= '0' and lastest[1] <= '3') or lastest[1] == '?':\n",
    "                lastest[0] = '2'\n",
    "            else:\n",
    "                lastest[0] = '1'\n",
    "\n",
    "        if lastest[1] == '?':\n",
    "            if lastest[0] == '2':\n",
    "                lastest[1] = '3'\n",
    "            else:\n",
    "                lastest[1] = '9'\n",
    "\n",
    "        if lastest[3] == '?':\n",
    "            lastest[3] = '5'\n",
    "\n",
    "        if lastest[4] == '?':\n",
    "            lastest[4] = '9'\n",
    "\n",
    "        return ''.join(lastest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        lists = []\n",
    "        for i in time:\n",
    "            lists.append(str(i))\n",
    "        for j in lists:\n",
    "            if lists[0] == '?':\n",
    "                if lists[1] == '?' or int(lists[1]) < 4:\n",
    "                    lists[0] = '2'\n",
    "                else:\n",
    "                    lists[0] = '1'\n",
    "            if lists[1] == '?' and lists[0] != '2':\n",
    "                lists[1] = '9'\n",
    "            elif lists[1] == '?' and lists[0] == '2':\n",
    "                lists[1] = '3'\n",
    "            if lists[3] == '?':\n",
    "                lists[3] = '5'\n",
    "            if lists[4] == '?':\n",
    "                lists[4] = '9'\n",
    "        time = ''.join(lists)\n",
    "        return time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        if time[0] == '?':\n",
    "                if time[1] > '3' and time[1] <= '9':\n",
    "                    time = time.replace('?', '1', 1)\n",
    "                else:\n",
    "                    time = time.replace('?', '2', 1)\n",
    "        if time[1] == '?':\n",
    "            if time[0] == '0' or time[0] == '1':\n",
    "                time = time.replace('?', '9', 1)\n",
    "            else:\n",
    "                time = time.replace('?', '3', 1)\n",
    "        if time[3] == '?':\n",
    "            time = time.replace('?','5', 1)\n",
    "        if time[4] == '?':\n",
    "            time = time.replace('?','9', 1)\n",
    "        return time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans = []\n",
    "        for i, ch in enumerate(time):\n",
    "            if ch == \"?\":\n",
    "                if i == 0:\n",
    "                    ans.append(\"2\" if time[1] == \"?\" or time[1] < \"4\" else \"1\")\n",
    "                if i == 1:\n",
    "                    ans.append(\"3\" if ans[0] == \"2\" else \"9\")\n",
    "                if i == 3:\n",
    "                    ans.append(\"5\")\n",
    "                if i == 4:\n",
    "                    ans.append(\"9\")\n",
    "            else:\n",
    "                ans.append(ch)\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        rst = \"\"\n",
    "        for i, x in enumerate(time):\n",
    "            if x != '?':\n",
    "                rst += x\n",
    "            elif i == 0:\n",
    "                if time[1] == '?':\n",
    "                    rst += '2'\n",
    "                elif time[1] >= '4':\n",
    "                    rst += '1'\n",
    "                else:\n",
    "                    rst += '2'\n",
    "            elif i == 1:\n",
    "                if rst[0] != '2':\n",
    "                    rst += '9'\n",
    "                else:\n",
    "                    rst += '3'\n",
    "            elif i == 3:\n",
    "                rst += '5'\n",
    "            else:\n",
    "                rst += '9'\n",
    "        return rst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for i in range(len(time)): \n",
    "            if time[i] == \"?\": \n",
    "                if i == 0: \n",
    "                    time[i] = \"2\" if time[i+1] in \"?0123\" else \"1\"\n",
    "                elif i == 1: \n",
    "                    time[i] = \"3\" if time[0] == \"2\" else \"9\"\n",
    "                elif i == 3: \n",
    "                    time[i] = \"5\"\n",
    "                else: \n",
    "                    time[i] = \"9\"\n",
    "                    \n",
    "        return \"\".join(time)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        [a,b,x,c,d] = time\n",
    "        if a == '?':\n",
    "            a = '2' if b == '?' or b < '4' else '1'\n",
    "        if b == '?':\n",
    "            b = '3' if a == '2' else '9'\n",
    "        if c == '?':\n",
    "            c = '5'\n",
    "        if d == '?':\n",
    "            d = '9'\n",
    "        return a + b + ':' + c +d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        a = time[:2]\n",
    "        b = time[-2:]\n",
    "        if a[0] =='?' and a[1] == '?':a = '23'\n",
    "        elif a[0] =='?' and a[1] != '?':\n",
    "            if int('2'+a[1]) < 24:a = '2' + a[1]\n",
    "            else:a = '1' + a[1]\n",
    "        elif a[0] != '?' and a[1] =='?':\n",
    "            if int(a[0]) < 2:a = a[0] + '9'\n",
    "            else:a = a[0] + '3'\n",
    "        \n",
    "        if b[0] =='?' and b[1] == '?':b = '59'\n",
    "        elif b[0] == '?' and b[1] != '?':b = '5' + b[1]\n",
    "        elif b[0] != '?' and b[1] =='?':b = b[0] +'9'\n",
    "\n",
    "        return a + ':' + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans=\"\"\n",
    "        h0=time[0]\n",
    "        h1=time[1]\n",
    "        if h0=='?' and h1=='?':\n",
    "            ans+=\"23\"\n",
    "        elif h0=='?' and h1!='?':\n",
    "            if int(h1)<=3:\n",
    "                ans+='2'+h1\n",
    "            else:\n",
    "                ans+='1'+h1\n",
    "        elif h0!='?' and h1=='?':\n",
    "            if int(h0)!=2:\n",
    "                ans+=h0+'9'\n",
    "            else:\n",
    "                ans+=h0+'3'\n",
    "        else:\n",
    "            ans+=time[:2]\n",
    "        ans+=':'\n",
    "        m0=time[3]\n",
    "        m1=time[4]\n",
    "        if m0=='?' and m1=='?':\n",
    "            ans+=\"59\"\n",
    "        elif m0=='?' and m1!='?':\n",
    "            ans+='5'+m1\n",
    "        elif m0!='?' and m1=='?':\n",
    "            if int(m0)==6:\n",
    "                ans+=m0+'0'\n",
    "            else:\n",
    "                ans+=m0+'9'\n",
    "        else:\n",
    "            ans+=time[-2:]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for idx, letter in enumerate(time):\n",
    "            if letter == '?':\n",
    "                if idx == 0:\n",
    "                    if time[1] == '?':\n",
    "                        time[0] = '2'\n",
    "                    elif int(time[1]) < 4:\n",
    "                        time[0] = '2'\n",
    "                    else:\n",
    "                        time[0] = '1'\n",
    "                if idx == 1:\n",
    "                    if time[0] == '0':\n",
    "                        time[1] = '9'\n",
    "                    elif time[0] == '1':\n",
    "                        time[1] = '9'\n",
    "                    elif time[0] == '2':\n",
    "                        time[1] = '3'\n",
    "                if idx == 3:\n",
    "                    time[3] = '5'\n",
    "                if idx == 4:\n",
    "                    time[4] = '9'\n",
    "        return ''.join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for i in range(len(time)):\n",
    "            if time[i]=='?':\n",
    "                if i==0:\n",
    "                    time[i]='2' if time[i+1] in '?0123' else'1'\n",
    "                elif i==1:\n",
    "                    time[i]='3' if time[0]=='2' else '9'\n",
    "                elif i==3:\n",
    "                    time[i]='5'\n",
    "                else:\n",
    "                    time[i]='9'\n",
    "        return ''.join(time)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "\n",
    "        time = list(time)\n",
    "        if time[0] == '?':\n",
    "            time[0] = '2' if time[1] in ('0','1','2','3','?') else '1'\n",
    "        if time[1] == '?':\n",
    "            time[1] = '9' if time[0] in ('0','1') else '3'\n",
    "        if time[3] == '?': time[3] = '5'\n",
    "        if time[4] == '?': time[4] = '9'\n",
    "        return \"\".join(time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans = ''\n",
    "        h, m = time.split(\":\")\n",
    "        # 小时\n",
    "        h = list(h)\n",
    "        if h[0] == '?' and h[1] == '?':\n",
    "            h[0], h[1] = '2', '3'\n",
    "        elif h[0] == '?':\n",
    "            if h[1] >= '4':\n",
    "                h[0] = '1'\n",
    "            else:\n",
    "                h[0] = '2'\n",
    "        elif h[1] == '?':\n",
    "            if h[0] <= '1':\n",
    "                h[1] = '9'\n",
    "            else:\n",
    "                h[1] = '3'\n",
    "       \n",
    "        m = list(m)\n",
    "        # 分钟\n",
    "        if m[0] == '?' and m[1] == '?':\n",
    "            m[0], m[1] = '5', '9'\n",
    "        elif m[0] == '?':\n",
    "            # if m[1] >= '4':\n",
    "            #     m[0] = '1'\n",
    "            # else:\n",
    "            m[0] = '5'\n",
    "        elif m[1] == '?':\n",
    "            # if m[0] <= '1':\n",
    "            #     m[0] = '9'\n",
    "            # else:\n",
    "            m[1] = '9'\n",
    "        return \"\".join(h) + ':' + \"\".join(m)\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans = list()\n",
    "        for i in range(5):\n",
    "            if time[i] == '?':\n",
    "                if i == 0:\n",
    "                    if ord(time[i + 1]) < ord('4') or time[i + 1] == '?': ans.append('2')\n",
    "                    else: ans.append('1')\n",
    "                elif i == 1:\n",
    "                    if ans[-1] == '2': ans.append('3')\n",
    "                    else: ans.append('9')\n",
    "                elif i == 3:\n",
    "                    ans.append('5')\n",
    "                else: ans.append('9')\n",
    "            else:\n",
    "                ans.append(time[i])\n",
    "        return \"\".join(a for a in ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans = []\n",
    "        for i, c in enumerate(time):\n",
    "            if c == \"?\":\n",
    "                if i == 0:\n",
    "                    if time[1] == \"?\" or time[1] < '4':\n",
    "                        ans.append(\"2\")\n",
    "                    else:\n",
    "                        ans.append(\"1\")\n",
    "                elif i == 1:\n",
    "                    if time[0] == \"?\" or time[0] == '2':\n",
    "                        ans.append(\"3\")\n",
    "                    else:\n",
    "                        ans.append(\"9\")\n",
    "                elif i == 3:\n",
    "                    ans.append(\"5\")\n",
    "                else:\n",
    "                    ans.append(\"9\")\n",
    "            else:\n",
    "                ans.append(c)\n",
    "        \n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "\n",
    "        result = []\n",
    "        for i, item in enumerate(time):\n",
    "            if item == \"?\":\n",
    "                if i == 3:\n",
    "                    result.append(\"5\")\n",
    "                elif i == 4:\n",
    "                    result.append(\"9\")\n",
    "                elif i == 0:\n",
    "                    if time[1] == '?' or int(time[1]) < 4:\n",
    "                        result.append(\"2\")\n",
    "                    else:\n",
    "                        result.append(\"1\")\n",
    "                else:\n",
    "                    if time[0] == '2' or time[0] == \"?\":\n",
    "                        result.append(\"3\")\n",
    "                    else:\n",
    "                        result.append(\"9\")\n",
    "\n",
    "            else:\n",
    "                result.append(item)\n",
    "        \n",
    "        return \"\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        return (('2' if time[1]=='?' or time[1]<'4' else '1') if time[0]=='?' else time[0])+(('3' if time[0]=='2' or time[0]=='?' else '9') if time[1]=='?' else time[1])+':'+('5' if time[3]=='?' else time[3])+('9' if time[4]=='?' else time[4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for i in range(len(time)): \n",
    "            if time[i] == \"?\": \n",
    "                if i == 0: \n",
    "                    time[i] = \"2\" if time[i+1] in \"?0123\" else \"1\"\n",
    "                elif i == 1: \n",
    "                    time[i] = \"3\" if time[0] == \"2\" else \"9\"\n",
    "                elif i == 3: \n",
    "                    time[i] = \"5\"\n",
    "                else: \n",
    "                    time[i] = \"9\"\n",
    "                    \n",
    "        return \"\".join(time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for i in range(len(time)): \n",
    "            if time[i] == \"?\": \n",
    "                if i == 0: \n",
    "                    time[i] = \"2\" if time[i+1] in \"?0123\" else \"1\"\n",
    "                elif i == 1: \n",
    "                    time[i] = \"3\" if time[0] == \"2\" else \"9\"\n",
    "                elif i == 3: \n",
    "                    time[i] = \"5\"\n",
    "                else: \n",
    "                    time[i] = \"9\"\n",
    "                    \n",
    "        return \"\".join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans = list()\n",
    "        for i in range(5):\n",
    "            if time[i] == '?':\n",
    "                if i == 0:\n",
    "                    if ord(time[i + 1]) < ord('4') or time[i + 1] == '?': ans.append('2')\n",
    "                    else: ans.append('1')\n",
    "                elif i == 1:\n",
    "                    if ans[-1] == '2': ans.append('3')\n",
    "                    else: ans.append('9')\n",
    "                elif i == 3:\n",
    "                    ans.append('5')\n",
    "                else: ans.append('9')\n",
    "            else:\n",
    "                ans.append(time[i])\n",
    "        return \"\".join(a for a in ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        n = len(time)\n",
    "        for index in range(n):\n",
    "            if index == 0 and time[index] == '?':\n",
    "                if time[index+1] == '?' or int(time[index+1]) < 4:\n",
    "                    time = time.replace('?', '2', 1)\n",
    "                elif int(time[index+1]) >= 4:\n",
    "                    time = time.replace('?', '1', 1)\n",
    "            elif index == 1 and time[index] == '?':\n",
    "                if int(time[index - 1]) < 2:\n",
    "                    time = time.replace('?', '9', 1)\n",
    "                else:\n",
    "                    time = time.replace('?', '3', 1)\n",
    "            elif index == 3 and time[index] == '?':\n",
    "                time = time.replace('?', '5', 1)\n",
    "            elif index == 4 and time[index] == '?':\n",
    "                time = time.replace('?', '9', 1)\n",
    "        return time\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        l1 = list(time)\n",
    "        x = \"\"\n",
    "        for i in range (len(l1)):\n",
    "            if l1[0] == \"?\":\n",
    "                if l1[1] != \"?\":\n",
    "                    if int(l1[1]) < 4:\n",
    "                        l1[0] = \"2\"\n",
    "                    else:\n",
    "                        l1[0] = \"1\"\n",
    "                else:\n",
    "                    l1[0] = \"2\"\n",
    "            if l1[1] == \"?\":\n",
    "                if l1[0] == \"2\":\n",
    "                    l1[1] = \"3\"\n",
    "                else:\n",
    "                    l1[1] = \"9\"\n",
    "            if l1[3] == \"?\":\n",
    "                l1[3] = \"5\"\n",
    "            if l1[4] == \"?\":\n",
    "                l1[4] = \"9\"\n",
    "        for i in range (len(l1)):\n",
    "            x += l1[i]\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:        \n",
    "        res=list(time)\n",
    "        if res[0]=='?' and res[1]!='?':\n",
    "            if int(res[1])<4:\n",
    "                res[0]='2'\n",
    "            else:\n",
    "                res[0]='1'\n",
    "        elif res[0]!='?' and res[1]=='?':\n",
    "            if int(res[0])<2:\n",
    "                res[1]='9'\n",
    "            else:\n",
    "                res[1]='3'\n",
    "        elif res[0]=='?' and res[1]=='?':\n",
    "            res[0]='2'\n",
    "            res[1]='3'\n",
    "        if res[3]=='?':\n",
    "            res[3]='5'\n",
    "        if res[4]=='?':                \n",
    "            res[4]='9'                \n",
    "        #print(res)\n",
    "        return ''.join(res)                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        n = len(time)\n",
    "        for i in range(n):\n",
    "            print(i, time[i])\n",
    "            if time[i] == '?':\n",
    "                if i == 0:\n",
    "                    if time[i + 1] == '?' or int(time[i + 1]) < 4:\n",
    "                        time = time.replace('?', '2', 1)\n",
    "                    else:\n",
    "                        time = time.replace('?', '1', 1)\n",
    "                elif i == 1:\n",
    "                    if time[i-1] == '2':\n",
    "                        time = time.replace('?', '3', 1)\n",
    "                    else:\n",
    "                        time = time.replace('?', '9', 1)\n",
    "                elif i == 3:\n",
    "                    time = time.replace('?', '5', 1)\n",
    "                else:\n",
    "                    time = time.replace('?', '9', 1)\n",
    "        return time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        i = 0\n",
    "        strs = [x for x in time]\n",
    "\n",
    "        for s in strs:\n",
    "            if strs[0] ==\"?\" and strs[1] ==\"?\":\n",
    "                strs[0] = \"2\"\n",
    "                strs[1] = \"3\"                \n",
    "            else:    \n",
    "                if s == \"?\" and i == 0:\n",
    "                    if strs[i + 1] < \"4\":\n",
    "                        strs[i] = \"2\"\n",
    "                    else:\n",
    "                        strs[i] = \"1\"\n",
    "                elif s == \"?\" and i == 1:\n",
    "                    if strs[i - 1] == \"2\":\n",
    "                        strs[i] = \"3\"\n",
    "                    else:\n",
    "                        strs[i] = \"9\"\n",
    "            if s == \"?\" and i == 3:\n",
    "                if strs[-1] == \"0\":\n",
    "                    strs[i] = \"5\"\n",
    "                else:\n",
    "                    strs[i] = \"5\"\n",
    "            elif s == \"?\" and i ==4:\n",
    "                if strs[i - 1] == \"6\":\n",
    "                    strs[i] = \"0\"\n",
    "                else:\n",
    "                    strs[i] = \"9\"\n",
    "            i += 1\n",
    "        string = \"\"\n",
    "        for h in strs:\n",
    "            string += h\n",
    "        return string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        if '?' not in time: return time\n",
    "        if time[0] == '?':\n",
    "            if time[1] > '3' and time[1] != '?':\n",
    "                ans = '1'\n",
    "            else:\n",
    "                ans = '2'\n",
    "        else:\n",
    "            ans = time[0]\n",
    "        if time[1] == '?': ans += ('3' if ans[0] == '2' else '9')\n",
    "        else: ans += time[1]\n",
    "        ans += ':'\n",
    "        ans += ('5' if time[3] == '?' else time[3])\n",
    "        ans += ('9' if time[-1] == '?' else time[-1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        if time[0] == '?':\n",
    "            time[0] = '2' if time[1] in ('0','1','2','3','?') else '1'\n",
    "        if time[1] == '?':\n",
    "            time[1] = '9' if time[0] in ('0','1') else '3'\n",
    "        if time[3] == '?': time[3] = '5'\n",
    "        if time[4] == '?': time[4] = '9'\n",
    "        return \"\".join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for i in range(len(time)): \n",
    "            if time[i] == \"?\": \n",
    "                if i == 0: \n",
    "                    time[i] = \"2\" if time[i+1] in \"?0123\" else \"1\"\n",
    "                elif i == 1: \n",
    "                    time[i] = \"3\" if time[0] == \"2\" else \"9\"\n",
    "                elif i == 3: \n",
    "                    time[i] = \"5\"\n",
    "                else: \n",
    "                    time[i] = \"9\"\n",
    "                    \n",
    "        return \"\".join(time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "\n",
    "        time = list(time)\n",
    "        for i in range(len(time)): \n",
    "            if time[i] == \"?\": \n",
    "                if i == 0: \n",
    "                    time[i] = \"2\" if time[i+1] in \"?0123\" else \"1\"\n",
    "                elif i == 1: \n",
    "                    time[i] = \"3\" if time[0] == \"2\" else \"9\"\n",
    "                elif i == 3: \n",
    "                    time[i] = \"5\"\n",
    "                else: \n",
    "                    time[i] = \"9\"\n",
    "                    \n",
    "        return \"\".join(time)\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        if time[0] == '?': time[0] = '2' if time[1] in ('0','1','2','3','?') else '1'\n",
    "        if time[1] == '?': time[1] = '9' if time[0] in ('0','1') else '3'\n",
    "        if time[3] == '?': time[3] = '5'\n",
    "        if time[4] == '?': time[4] = '9'\n",
    "        return \"\".join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = [x for x in time]\n",
    "        if time[4] == '?':\n",
    "            time[4] = '9'\n",
    "        if time[3] == '?':\n",
    "            time[3] = '5'\n",
    "        if time[0] == '?':\n",
    "            if time[1] < '4' or time[1] == '?':\n",
    "                time[0] = '2'\n",
    "            else:\n",
    "                time[0] = '1'\n",
    "        if time[1] == '?':\n",
    "            if time[0] < '2':\n",
    "                time[1] = '9'\n",
    "            else:\n",
    "                time[1] = '3'\n",
    "        return ''.join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time=list(time)\n",
    "        if time[0]=='?':\n",
    "            if time[1]>='4' and time[1]!='?':\n",
    "                time[0]='1'\n",
    "            if time[1]<'4' and time[1]!='?':\n",
    "                time[0]='2'\n",
    "            if time[1]=='?':\n",
    "                time[0], time[1]='2', '3'\n",
    "        if time[1]=='?':\n",
    "            if time[0]<='1':\n",
    "                time[1]='9'\n",
    "            else:\n",
    "                time[1]='3'\n",
    "        if time[3]=='?':\n",
    "            time[3]='5'\n",
    "        if time[4]=='?':\n",
    "            time[4]='9'\n",
    "        return ''.join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ans = ''\n",
    "        for i in range(24):\n",
    "            for j in range(60):\n",
    "                t = '%02i:%02i' % (i, j)\n",
    "                if all(t[k] == time[k] or time[k] == '?' for k in range(5)):\n",
    "                    ans = t\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        t = list(time)\n",
    "        lst = []\n",
    "        for i in range(len(time)):\n",
    "            if time[i] == \"?\":\n",
    "                lst.append(i)\n",
    "        for n in lst:\n",
    "            if n == 0:\n",
    "                if t[1] != \"?\" and int(t[1]) > 3:\n",
    "                    t[n] = \"1\"\n",
    "                else:\n",
    "                    t[n] = \"2\"\n",
    "            elif n == 1:\n",
    "                t[n] = \"9\" if t[0] == \"1\" or t[0] == \"0\" else \"3\"\n",
    "            elif n == 3:\n",
    "                t[n] = \"5\"\n",
    "            elif n == 4:\n",
    "                t[n] = \"9\"\n",
    "        return \"\".join(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        if time[0]=='?':\n",
    "            if time[1]<'4' or time[1]=='?':\n",
    "                time[0]='2'\n",
    "            else:\n",
    "                time[0]='1'\n",
    "        if time[1]=='?':\n",
    "            if time[0]=='2':\n",
    "                time[1]='3'\n",
    "            else:\n",
    "                time[1]='9'\n",
    "        if time[3]=='?':\n",
    "            time[3]='5'\n",
    "        if time[4]=='?':\n",
    "            time[4]='9'\n",
    "        return ''.join(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        [a,b,_,c,d] = time\n",
    "        if a == '?':\n",
    "            a ='2' if b=='?' or b<'4' else '1'\n",
    "        if b == '?':\n",
    "            b ='3' if a=='2' else '9'\n",
    "        if c == '?':\n",
    "            c = '5'\n",
    "        if d == '?':\n",
    "            d = '9'\n",
    "        return f'{a}{b}:{c}{d}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        x=time[:2]\n",
    "        y=time[3:]\n",
    "        if '?' in x:\n",
    "            if '?'==x[1]:\n",
    "                if x[0]=='2':\n",
    "                    x=x.replace('?','3')\n",
    "                elif x[0] in ['1','0']:\n",
    "                    x=x.replace('?','9')\n",
    "                else:\n",
    "                    x='23'\n",
    "            elif '?'==x[0]:\n",
    "                if int(x[1])>=4:\n",
    "                    x=x.replace('?', '1')\n",
    "                else:\n",
    "                    x=x.replace('?', '2')\n",
    "        if '?' in y:\n",
    "            if y[0]==y[1]=='?':\n",
    "                y='59'\n",
    "            elif '?'==y[1]:\n",
    "                y=y.replace('?', '9')\n",
    "            elif '?'==y[0]:\n",
    "                y=y.replace('?', '5')\n",
    "        return x+':'+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        i = 0\n",
    "        time1 = list(time)\n",
    "        while i < len(time1):\n",
    "            if time1[i] == '?':\n",
    "                match i:\n",
    "                    case 0:\n",
    "                        if time1[1]=='?':\n",
    "                            time1[i] = '2'\n",
    "                        elif int(time1[1])<4:\n",
    "                            time1[i] = '2'\n",
    "                        else:\n",
    "                            time1[i] = '1'\n",
    "                    case 1:\n",
    "                        if time1[0] == '2':\n",
    "                            time1[i] = '3'\n",
    "                        else:\n",
    "                            time1[i] = '9'\n",
    "                    case 3:\n",
    "                        time1[i] = '5'\n",
    "                    case 4:\n",
    "                        time1[i] = '9'\n",
    "            i += 1\n",
    "        print(time1)\n",
    "        time1 = [str(item) for item in time1]\n",
    "        time2 = ''.join(time1)\n",
    "        return time2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        \n",
    "        res = list(time)\n",
    "        i = 0\n",
    "        if time[i] == \"?\" and time[i+1] == \"?\":\n",
    "            res[:i+2] = \"23\"\n",
    "        elif time[i] == \"?\":\n",
    "            if int(time[i+1]) > 3:\n",
    "                res[i] = \"1\"\n",
    "            else:\n",
    "                res[i] = \"2\"\n",
    "        elif time[i+1] == \"?\":\n",
    "            if int(time[i]) == 2:\n",
    "                res[i+1] = \"3\"\n",
    "            else:\n",
    "                res[i+1] = \"9\"\n",
    "        i = 3\n",
    "        if time[i] == \"?\":\n",
    "            res[i] = \"5\"\n",
    "        if time[i+1] == \"?\":\n",
    "            res[i+1] = \"9\"\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        hour,time=time.split(':')\n",
    "        a,b=hour\n",
    "        c,d=time\n",
    "\n",
    "        if a==\"?\":\n",
    "            if b<\"4\":\n",
    "                a=2\n",
    "            elif b==\"?\":\n",
    "                a=2\n",
    "                b=3\n",
    "            elif b>\"3\":\n",
    "                a=1\n",
    "        if b==\"?\":\n",
    "            if a==\"2\":\n",
    "                b=3\n",
    "            else:\n",
    "                b=9\n",
    "        if c==\"?\":\n",
    "            c=5\n",
    "        if d==\"?\":\n",
    "            d=9\n",
    "\n",
    "        return f\"{a}{b}:{c}{d}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        for i in range(len(time)): \n",
    "            if time[i] == \"?\": \n",
    "                if i == 0: \n",
    "                    time[i] = \"2\" if time[i+1] in \"?0123\" else \"1\"\n",
    "                elif i == 1: \n",
    "                    time[i] = \"3\" if time[0] == \"2\" else \"9\"\n",
    "                elif i == 3: \n",
    "                    time[i] = \"5\"\n",
    "                else: \n",
    "                    time[i] = \"9\"\n",
    "                    \n",
    "        return \"\".join(time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        time = list(time)\n",
    "        if time[0] == '?':\n",
    "            time[0] = '2' if time[1] in ('0','1','2','3','?') else '1'\n",
    "        if time[1] == '?':\n",
    "            time[1] = '9' if time[0] in ('0','1') else '3'\n",
    "        if time[3] == '?': time[3] = '5'\n",
    "        if time[4] == '?': time[4] = '9'\n",
    "        return \"\".join(time)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        hours,minutes = time.split(':')\n",
    "        hours = list(hours)\n",
    "        minutes = list(minutes)\n",
    "        if hours[0]=='?' and hours[1] in ['0','1','2','3','?']:hours[0] = '2'\n",
    "        elif hours[0]=='?' and hours[1] not in ['0','1','2','3','?']:hours[0] = '1'\n",
    "        if hours[1]=='?' and hours[0]!='2':hours[1] = '9'\n",
    "        elif hours[1]=='?' and hours[0]=='2':hours[1] = '3'\n",
    "        if minutes[0]=='?':minutes[0] = '5'\n",
    "        if minutes[1]=='?':minutes[1] = '9'\n",
    "        return ''.join(hours)+':'+''.join(minutes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        for i in range(5):\n",
    "            if time[i]=='?':\n",
    "                if i==0:\n",
    "                    if time[i+1]>='4' and time[i+1]<='9':\n",
    "                        time='1'+time[1:]\n",
    "                    else:\n",
    "                        time='2'+time[1:]\n",
    "                elif i==1:\n",
    "                    if time[i-1]=='2':\n",
    "                        time=time[:i]+'3'+time[i+1:]\n",
    "                    else:\n",
    "                        time=time[:i]+'9'+time[i+1:]\n",
    "                elif i==3:\n",
    "                    time=time[:i]+'5'+time[i+1:]\n",
    "                elif i==4:\n",
    "                    time=time[:i]+'9'\n",
    "        return time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        x=time[:2]\n",
    "        y=time[3:]\n",
    "        if '?' in x:\n",
    "            if '?'==x[1]:\n",
    "                if x[0]=='2':\n",
    "                    x=x.replace('?','3')\n",
    "                elif x[0] in ['1','0']:\n",
    "                    x=x.replace('?','9')\n",
    "                else:\n",
    "                    x='23'\n",
    "            elif '?'==x[0]:\n",
    "                if int(x[1])>=4:\n",
    "                    x=x.replace('?', '1')\n",
    "                else:\n",
    "                    x=x.replace('?', '2')\n",
    "        if '?' in y:\n",
    "            if y[0]==y[1]=='?':\n",
    "                y='59'\n",
    "            elif '?'==y[1]:\n",
    "                y=y.replace('?', '9')\n",
    "            elif '?'==y[0]:\n",
    "                y=y.replace('?', '5')\n",
    "        return x+':'+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        stime_l = list(time)\n",
    "        if stime_l[-1] == '?':\n",
    "            stime_l[-1] = '9'\n",
    "        if stime_l[-2] == '?':\n",
    "            stime_l[-2] = '5'\n",
    "\n",
    "        if stime_l[1] == '?':\n",
    "            if stime_l[0] == '?':\n",
    "                stime_l[0] = '2'\n",
    "                stime_l[1] = '3'\n",
    "            elif stime_l[0] in ['0','1']:\n",
    "                stime_l[1] = '9'\n",
    "            else:\n",
    "                stime_l[1] = '3'\n",
    "        elif stime_l[1] in ['0','1','2','3']:\n",
    "            if stime_l[0] == '?':\n",
    "                stime_l[0] = '2'\n",
    "        else:\n",
    "            if stime_l[0] == '?':\n",
    "                stime_l[0] = '1'\n",
    "        return ''.join(stime_l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        ret =\"\"\n",
    "        def verify(a,b):\n",
    "            if str(a) == b or  b == \"?\":\n",
    "                return True\n",
    "            return False\n",
    "        for i in range(1440):\n",
    "            a1= i//60//10\n",
    "            a2 = i//60%10\n",
    "            a3 = i %60//10\n",
    "            a4 = i%60%10\n",
    "            if verify(a1,time[0]) and verify(a2,time[1]) and verify(a3,time[3]) and verify(a4,time[4]):\n",
    "                ret = str(a1) + str(a2) + \":\" +str(a3)+str(a4)\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        hh, mm = time.split(':')\n",
    "\n",
    "        h1, h2 = hh[0], hh[1]\n",
    "        if h1 == '?':\n",
    "            if h2 != '?':\n",
    "                if int(h2) <= 3:\n",
    "                    h1 = 2\n",
    "                else:\n",
    "                    h1 = 1\n",
    "            else:\n",
    "                h1 = 2\n",
    "                h2 = 3\n",
    "        else:\n",
    "            if int(h1) <= 1:\n",
    "                if h2 == '?':\n",
    "                    h2 = 9\n",
    "            else:\n",
    "                if h2 == '?':\n",
    "                    h2 = 3\n",
    "\n",
    "        m1, m2 = mm[0], mm[1]\n",
    "        if m1 == '?':\n",
    "            m1 = 5\n",
    "        if m2 == '?':\n",
    "            m2 = 9\n",
    "\n",
    "        return '{}{}:{}{}'.format(h1, h2, m1, m2)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTime(self, time: str) -> str:\n",
    "        for i in range(0, len(time)):\n",
    "            if time[i] == '?':\n",
    "                if i == 0:\n",
    "                    if time[1] in ['?', '0', '1', '2', '3']:\n",
    "                        time = '2' + time[1:]\n",
    "                    else:\n",
    "                        time = '1' + time[1:]\n",
    "                elif i == 1:\n",
    "                    if time[0] == '2':\n",
    "                        time = time[:1] + '3' + time[2:]\n",
    "                    else:\n",
    "                        time = time[:1] + '9' + time[2:]\n",
    "                elif i == 3:\n",
    "                    time = time[:3] + '5' + time[4:]\n",
    "                elif i == 4:\n",
    "                    time = time[:4] + '9'\n",
    "        return time\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
